﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ControllerLag;
using Model;

namespace Projekt_1
{
    public partial class FrmVareListe : Form
    {
        Controller controller;

        public FrmVareListe(Controller controller)
        {
            InitializeComponent();
            this.controller = controller;
            UpdateGUI();
        }

        private void btnNyVare_Click(object sender, EventArgs e)
        {
            OpretVare form = new OpretVare(controller);
            form.ShowDialog();

            UpdateGUI();
        }

        private void btnRetVare_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex;

                if (int.TryParse(lstVarer.SelectedItems[0].Text, out selectedIndex))
                {
                    FrmRetVare form = new FrmRetVare(controller, controller.FindVare(selectedIndex));
                    form.ShowDialog();
                }
                else
                {
                    MessageBox.Show("Vælg en vare");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }

            UpdateGUI();
        }




        private void btnLuk_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void UpdateGUI()
        {
            lstVarer.Items.Clear();

            foreach (Vare v in controller.HentVareliste())
            {
                ListViewItem i = new ListViewItem();
                i.Text = v.VareNr.ToString();
                i.SubItems.Add(v.Navn);
                i.SubItems.Add(v.Beskrivelse);
                i.SubItems.Add(v.Pris.ToString());
                if (v.Aktiv)
                {
                    i.SubItems.Add("Ja");
                }
                else
                {
                    i.SubItems.Add("Nej");
                }
                lstVarer.Items.Add(i);
            }
        }



    }
}
